<html>
<head><meta charset="utf-8"><title>transmute and repr(transparent) · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html">transmute and repr(transparent)</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="178689506"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178689506" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178689506">(Oct 21 2019 at 19:34)</a>:</h4>
<p>It's safe to transmute <code>Vec&lt;Foo&lt;T&gt;&gt;</code> to <code>Vec&lt;T&gt;</code> if <code>Foo</code> is <code>repr(transparent)</code>, yeah?</p>



<a name="178691017"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178691017" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178691017">(Oct 21 2019 at 19:50)</a>:</h4>
<p>and I assume <code>struct Foo(T)</code>?</p>



<a name="178691120"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178691120" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178691120">(Oct 21 2019 at 19:51)</a>:</h4>
<p>I think yes (but beware an extra <code>Drop</code> impl for <code>Foo</code>)</p>



<a name="178691135"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178691135" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178691135">(Oct 21 2019 at 19:51)</a>:</h4>
<p>also see <a href="https://github.com/rust-lang/rfcs/pull/2756" target="_blank" title="https://github.com/rust-lang/rfcs/pull/2756">https://github.com/rust-lang/rfcs/pull/2756</a></p>



<a name="178691429"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178691429" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178691429">(Oct 21 2019 at 19:53)</a>:</h4>
<p>Should <code>miri</code> detect a missing <code>#[repr(transparent)]</code>?</p>



<a name="178691489"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178691489" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178691489">(Oct 21 2019 at 19:54)</a>:</h4>
<p>no, miri doesn't detect relying on unspecified layout details</p>



<a name="178691571"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178691571" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178691571">(Oct 21 2019 at 19:55)</a>:</h4>
<p>we could conceivably add a flag to randomize <code>repr(Rust)</code> layout and add some redundant padding or so to detect such cases, but currently we dont have anything like that</p>



<a name="178691593"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178691593" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178691593">(Oct 21 2019 at 19:55)</a>:</h4>
<p>Ah, okay</p>



<a name="178691595"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178691595" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178691595">(Oct 21 2019 at 19:55)</a>:</h4>
<p>I don't believe we've ever made a commitment wrt. type constructors in this way</p>



<a name="178691842"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178691842" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178691842">(Oct 21 2019 at 19:57)</a>:</h4>
<p>well it kind of follows from <code>repr(transparent)</code> allowing transmutes</p>



<a name="178691871"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178691871" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178691871">(Oct 21 2019 at 19:57)</a>:</h4>
<p>and the fact that <code>Vec</code> extensively documents its internals</p>



<a name="178691911"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178691911" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178691911">(Oct 21 2019 at 19:58)</a>:</h4>
<p>if you wanted to be really safe you'd go via <code>to/from_raw_parts</code></p>



<a name="178691975"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178691975" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178691975">(Oct 21 2019 at 19:58)</a>:</h4>
<p>well probably you should actually, given <a href="https://github.com/rust-lang/rust-clippy/issues/4484" target="_blank" title="https://github.com/rust-lang/rust-clippy/issues/4484">https://github.com/rust-lang/rust-clippy/issues/4484</a> ^^</p>



<a name="178691994"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178691994" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178691994">(Oct 21 2019 at 19:58)</a>:</h4>
<p>also see <a href="https://github.com/rust-lang/rust/issues/64073" target="_blank" title="https://github.com/rust-lang/rust/issues/64073">https://github.com/rust-lang/rust/issues/64073</a></p>



<a name="178693465"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178693465" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178693465">(Oct 21 2019 at 20:11)</a>:</h4>
<blockquote>
<p>and I assume <code>struct Foo(T)</code>?</p>
</blockquote>
<p><code>struct Foo&lt;T&gt; { value: T } </code> (but <code>repr(transparent)</code> enforces only one value, IIRC)</p>



<a name="178694122"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178694122" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178694122">(Oct 21 2019 at 20:18)</a>:</h4>
<p>As a general principle, transmuting under type constructors is plainly incorrect: associated types and constants as well as various parametricity-piercing widgets can make <code>Foo&lt;Wrapper&lt;T&gt;&gt;</code> and <code>Foo&lt;T&gt;</code> differ<br>
So it's always up to what the specific <code>Foo</code> (in this case <code>Vec</code>) guarantees</p>



<a name="178694185"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178694185" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178694185">(Oct 21 2019 at 20:19)</a>:</h4>
<p><code>Vec</code> could make guarantee that it only depends on size and alignment for these matters, but it currently doesn't, so the transmute is at least "library UB"</p>



<a name="178694876"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178694876" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178694876">(Oct 21 2019 at 20:25)</a>:</h4>
<p>I wish there was a <code>Vec::to_raw_parts</code></p>



<a name="178695034"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178695034" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178695034">(Oct 21 2019 at 20:27)</a>:</h4>
<p>I'm happy I asked. I was 95% sure it was fine.</p>



<a name="178695157"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178695157" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178695157">(Oct 21 2019 at 20:28)</a>:</h4>
<blockquote>
<p>I wish there was a <code>Vec::to_raw_parts</code></p>
</blockquote>
<p>This feels like an API that we can and should add</p>



<a name="178695254"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178695254" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178695254">(Oct 21 2019 at 20:29)</a>:</h4>
<p>As I said it, I was wondering what it'd return. A tuple? A little struct?</p>



<a name="178695291"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178695291" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178695291">(Oct 21 2019 at 20:29)</a>:</h4>
<p>I guess a tuple would be fine</p>



<a name="178695600"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178695600" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178695600">(Oct 21 2019 at 20:32)</a>:</h4>
<p>tuple, I'd say, to let you easily destructure and pass to from_raw_parts</p>



<a name="178695955"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178695955" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178695955">(Oct 21 2019 at 20:36)</a>:</h4>
<p>Seems sensible</p>



<a name="178698907"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178698907" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178698907">(Oct 21 2019 at 21:06)</a>:</h4>
<p>To be easier to use correctly than just writing out <code>(v.as_ptr(), v.len(), v.capacity())</code> (which requires <code>mem::forget</code> afterwards) it should probably be <code>into_raw_parts(self)</code></p>



<a name="178757654"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178757654" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178757654">(Oct 22 2019 at 13:51)</a>:</h4>
<p>Alrighty, I'm running the tests locally (also added it for <code>String</code>), will push when those pass.</p>



<a name="178764855"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178764855" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178764855">(Oct 22 2019 at 15:08)</a>:</h4>
<p>And by "tests" I really mean building rust / LLVM so hard that my screen glitches and my machine restarted</p>



<a name="178771025"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178771025" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178771025">(Oct 22 2019 at 16:16)</a>:</h4>
<p>for a PR to add those methods?</p>



<a name="178773767"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178773767" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178773767">(Oct 22 2019 at 16:50)</a>:</h4>
<p>yep</p>



<a name="178773796"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178773796" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178773796">(Oct 22 2019 at 16:51)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/pull/65705" target="_blank" title="https://github.com/rust-lang/rust/pull/65705">https://github.com/rust-lang/rust/pull/65705</a></p>



<a name="178793688"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178793688" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178793688">(Oct 22 2019 at 20:04)</a>:</h4>
<p>these LLVM builds also feel like they take longer and longer... and compared to earlier times I have to rebuild LLVM much more often, too</p>



<a name="178794304"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178794304" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178794304">(Oct 22 2019 at 20:11)</a>:</h4>
<p>I'm doing <code>../x.py --jobs 3 test src/liballoc/</code> and one thing I always know is that there's some faster way to run the tests I want, but I never know what.</p>



<a name="178794326"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/178794326" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#178794326">(Oct 22 2019 at 20:11)</a>:</h4>
<p>(set to 3 to allow me to use my computer for other things)</p>



<a name="179075852"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20and%20repr%28transparent%29/near/179075852" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20and.20repr(transparent).html#179075852">(Oct 25 2019 at 18:20)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> did you edit rustc at all? if you only edit libstd you can do stage 0 tests... add <code>--stage 0</code></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>